package org.androidpn.server.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;

import org.androidpn.server.dao.FriendMessageDao;
import org.androidpn.server.entity.Friend;
import org.androidpn.server.entity.FriendMessage;
import org.androidpn.server.entity.FriendMessage.FriendMessageStatus;
import org.springframework.stereotype.Repository;

/**
 * Dao FriendMessage
 * 
 * @author 蔡琪
 * 
 */
@Repository("friendMessageDao")
public class FriendMessageDaoImpl extends BaseDaoImpl<FriendMessage, Long>
		implements FriendMessageDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.androidpn.server.dao.FriendMessageDao#findNewFriendMessagesByFriendId
	 * (java.lang.Long, java.lang.Long)
	 */
	public List<FriendMessage> findNewFriendMessages(Long senderId,
			Long receiverId) {
		String jpql = "from FriendMessage msg " + "where "
				+ "msg.sender.id = :senderId "
				+ "and msg.receiver.id=:receiverId "
				+ "and msg.friendMessageStatus=:friendMessageStatus "
				+ "order by msg.createDate desc";
		TypedQuery<FriendMessage> query = entityManager
				.createQuery(jpql, FriendMessage.class)
				.setParameter("senderId", senderId)
				.setParameter("receiverId", receiverId)
				.setParameter("friendMessageStatus",
						FriendMessageStatus.sending);
		return query.getResultList();
	}

	public List<FriendMessage> findFriendMessagesByReceiverId(Long roleId) {
		String jpql = "from FriendMessage msg " + "where "
				+ "msg.receiver.id = :roleId "
				+ "and msg.friendMessageStatus=:friendMessageStatus "
				+ "order by msg.createDate desc";
		TypedQuery<FriendMessage> query = entityManager
				.createQuery(jpql, FriendMessage.class)
				.setParameter("roleId", roleId)
				.setParameter("friendMessageStatus",
						FriendMessageStatus.sending);
		List<FriendMessage> friendMessages = query.getResultList();
		return friendMessages;
	}

	public List<FriendMessage> findNewFriendMessagesBySender(Long friendId,
			Long roleId) {
		return null;
	}

}
